var userStore = new Ext.data.JsonStore({
	url: '/weihe/getList_user',
	root: 'datas',
	fields : [{
				name:'UserId',mapping:'id'
			},{
				name:'UserUsername',mapping:'username'
			},{
				name:'UserPassword',mapping:'password'
			},{
				name:'UserDescn',mapping:'descn'
			}],
			listeners :{ 
				   'beforeload':function(s,options) {
						var tag = 0;
						Ext.Ajax.request({
							url : '/weihe/getListJudge_user',
							success : function(response) {
								var json = Ext.decode(response.responseText);
								if (json.deny) {
									Ext.Msg.alert('错误',json.msg);
									tag = 1;
								}
									if(tag == 1){
										return false;
									}else{
										return true;						
									}
							},
							failure : function() {
								Ext.Msg.alert('错误', '失败!');
								tag =1;
							}
						});
					
			   }
			}
		});



user = function(){
		return{
		
		userGrid : function(){
		
			var pageToolBar = new Ext.PagingToolbar({
				pageSize : 15,
				store : userStore,
				displayInfo : true,
				displayMsg : '当前显示{0}-{1}条记录,共{2}条记录',
			    emptyMsg : "没有记录显示!",
			    firstText : '首页',
			    prevText : '上一页',
			    nextText : '下一页',
			    lastText : '尾页',
			    beforePageText : '第',
			    afterPageText : '页,共{0}页'
			   
			});
			
			
			var rowSelectionModel = new Ext.grid.RowSelectionModel({singleSelect : true});
			
			var userGrid = new Ext.grid.GridPanel({
			   id:'userGrid',
			   title : '用户信息表',
			   region : 'center',
			   minColumnWidth : 50,
			   loadMask : {msg:'正在加载数据，请稍侯...'},
			   store : userStore,
			   columns: [
			             {header : '用户名', dataIndex : 'UserUsername'},
			             {header : '密码', dataIndex : 'UserPassword'},
			             {header : '描述', dataIndex : 'UserDescn'}
			],
			   sm : rowSelectionModel,
			   listeners :{ 
				   'rowclick':function(grid, rowIndex, event) {
				   var record = grid.getStore().getAt(rowIndex);
				   Ext.getCmp("userForm").form.loadRecord(record);
				   Ext.getCmp("userForm").form.findField("up-confirm").setValue(record.get('UserPassword'));
				   Ext.getCmp("userForm").buttons[0].setText('修改');
			   }
			},
			  viewConfig : {
			     forceFit : false
			  },
			  bbar : pageToolBar
			});
		return userGrid;
		},
		userForm : function(){
			
			var button_add = new Ext.Button({
				text : '添加',
				handler : function() {
			
						progressConsole();
					if(userForm.form.isValid()){ 	
						if (userForm.form.findField("user.id").getValue() == "") {
							userForm.form.submit({
							  url : '/weihe/add_user',
							   success : function(form, action) {
								 if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								  }else{
										Ext.Msg.alert('消息', action.result.msg, function() {
											userForm.form.reset();
											   Ext.getCmp("userGrid").getStore().reload();
									     });
								  }
							    }
						    });
						}else {
							userForm.form.submit({
								  url : '/weihe/update_user',
								   success : function(form, action) {
									 if (action.result.success) {
										Ext.Msg.alert('消息', action.result.msg, function() {
											userForm.form.reset();
											Ext.getCmp("userGrid").getStore().reload();
											userForm.buttons[0].setText('添加');
											
										});
									  }
								    }
							    });
						}
				}else{
					Ext.Msg.alert('提示','请输入相关内容');
				}
					}
			
				
			});
			
			var button_clear = new Ext.Button({
			id : 'button_clear',
			text : '清空',
			handler : function() {
				userForm.form.reset();
				userForm.buttons[0].setText('添加');
			}
			});
			
			var button_del = new Ext.Button({
			text : '删除',
			handler : function() {
				var id = userForm.form.findField('user.id').getValue();
				if (id == '') {
					Ext.Msg.alert('提示', '请选择需要删除的信息!');
				} else {
					progressConsole();
					Ext.Ajax.request({
						url : '/weihe/delete_user',
						success : function(response) {
							var json = Ext.decode(response.responseText);
							if (json.success) {
								Ext.Msg.alert('消息', json.msg, function() {
									Ext.getCmp("userGrid").getStore().load({params : {start : 0, limit : 15}});
									userForm.form.reset();
									userForm.buttons[0].setText('添加');
								});
							}else{
								Ext.Msg.alert('消息', json.msg);
								userForm.form.reset();
								userForm.buttons[0].setText('添加');
							}
						},
						failure : function() {
							Ext.Msg.alert('错误', '删除失败!');
						},
						params : 'user.id=' + id
					});
				}
			}
			});
			Ext.apply(Ext.form.VTypes, {
			password: function(val, field) {
			if (field.initialPassField) {
			var pwd = Ext.getCmp(field.initialPassField);
			return (val == pwd.getValue());
			}
			return true;
			},
			passwordText: '两次输入的密码不相同!'
			});
			
			
			
			var  userForm = new Ext.form.FormPanel({
			id : 'userForm',
			title : '用户信息',
			frame : true,
			width : 350,
			region : 'east',
			autoScroll : true,
			labelAlign : 'right',
			labelWidth : 80,
			defaultType : 'textfield',
				defaults : {
				width : 200
			},
			items : [{  // Ext.form.Hidden
				id:'UserId',
				xtype : 'hidden',  // 隐藏的表单
				name : 'user.id'
			},{
				id:'UserUsername',
				fieldLabel : '用户名',
				name : 'user.username',
				allowBlank:false,
				blankText:'请输入用户名'
			},{
				id:'UserPassword',
				fieldLabel : '密码',
				inputType: 'password',
				name : 'user.password',
				allowBlank:false,
				blankText:'请输入密码'
			},{ 
				id: 'up-confirm',
				fieldLabel: '密码确认',
				inputType: 'password',
				allowBlank: false,
				vtype: 'password', initialPassField: 'UserPassword'
			},{
				id:'UserDescn',
				fieldLabel : '描述',
				name : 'user.descn',
				allowBlank:false,
				blankText:'请输入描述'
			}],
			buttons : [button_add,button_clear,button_del]
			});
		return  userForm;
		}
	}
}();














